<template>
    <SysDialog :title="dialog.title" :visible="dialog.visible" :width="dialog.width" :height="dialog.height"
        @onClose="onClose" @onConfirm="commit">
        <template v-slot:content>
            <el-form :model="addModel" ref="addFormRef" label-width="90px" :inline="false" size="default">
                <!-- <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="tuHao" label="图号" label-width="120px">
                            <el-input v-model="addModel.tuHao"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="name" label="名称">
                            <el-input v-model="addModel.name"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="caiLiao" label="材料">
                            <el-input v-model="addModel.caiLiao"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="num" label="数量">
                            <el-input v-model="addModel.num"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="age" label="库龄">
                            <el-input v-model="addModel.age"></el-input>
                        </el-form-item>

                    </el-col>
                </el-row>
            </el-form> -->



                <el-row :gutter="20" justify="center">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="tuHao" label="图号">
                            <el-input v-model="addModel.tuHao"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="name" label="名称">
                            <el-input v-model="addModel.name"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20" justify="center">
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="caiLiao" label="材料">
                            <el-input v-model="addModel.caiLiao"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="num" label="数量">
                            <el-input v-model="addModel.num"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20" justify="center" >
                    <el-col :span="8" :offset="0">
                        <el-form-item prop="age" label="库龄">
                            <el-input v-model="addModel.age"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0"></el-col>
                </el-row>
            </el-form>
        </template>
    </SysDialog>
</template>

<script setup lang="ts">

import SysDialog from '@/components/SysDialog.vue'
import useDialog from '@/hooks/useDialog';
import { nextTick, reactive, ref } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import { addUserApi, editUserApi, getListApi } from '@/api/xsjk';
import { EditType, Title } from '@/type/BaseType';
import { XsjkModel } from '@/api/xsjk/XsjkModel';
const { dialog, onClose, onConfirm, onShow } = useDialog()    //弹框属性
const show = (type: string, row?: XsjkModel) => {//显示弹窗

    type == EditType.ADD ? dialog.title = Title.ADD : dialog.title = Title.EDIT
    dialog.height = 200
    dialog.width = 800
    dialog.visible = true

    if (row) {
        nextTick(() => {
            Object.assign(addModel, row)
            console.log('addModel:', addModel);
        })
    }

    addFormRef.value?.resetFields()

    addModel.type = type;
};

defineExpose({
    show
})

const addModel = reactive<XsjkModel>({
    type: '',
    id: '',
    tuHao: '',
    name: '',
    caiLiao: '',
    num: '',
    age: '',
    daiZhi:'',
})

const addFormRef = ref<FormInstance>()


const emists = defineEmits(['onFresh'])


const commit = () => {
    addFormRef.value?.validate(async (valid) => {
        if (valid) {
            let res = null;
            if (addModel.type == EditType.ADD) {
                res = await addUserApi(addModel);
            } else {
                res = await editUserApi(addModel);
            }
            console.log(res)
            if (res && res.code == 200) {
                ElMessage.success(res.msg)
                emists('onFresh')
                onClose()
            }
        }
    })
}
</script>
<style scoped></style>